<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('店铺加油订单列表')"/>
    <th:block th:include="include :: bootstrap-select-css" />

</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="petrol-order-form">
                <div class="select-list">
                    <ul>
                        <li>
                            <p style="width: 110px">手机号： </p>
                            <input type="text" name="phone"/>
                        </li>
                        <li>
                            <p style="width: 110px">订单号： </p>
                            <input type="text" name="orderSn"/>
                        </li>
                        <li>
                            <p style="width: 110px">订单状态： </p>
                            <select name="orderStatus" th:with="type=${@dict.getType('order_status')}">
                                <option value="-1">全部</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <p style="width: 110px">付款状态： </p>
                            <select name="payStatus" th:with="type=${@dict.getType('pay_status')}">
                                <option value="-1">全部</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <p style="width: 110px">支付方式： </p>
                            <select name="payWay" th:with="type=${@dict.getType('pay_way')}">
                                <option value="-1">全部</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <p style="width: 110px;">油品型号：</p>
                            <select id="productIdStr" th:with="petrol=${petrolTypeList}" class="selectpicker" data-none-selected-text="请选择" multiple>
                                <option th:each="one : ${petrol}" th:text="${one.productName}" th:value="${one.id}"></option>
                            </select>
                        </li>
                        <li>
                            <p style="width: 110px">油枪号： </p>
                            <select name="productAttrId" th:with="productAttrList=${productAttrList}">
                                <option value="">全部</option>
                                <option th:each="attr:${productAttrList}" th:value="${attr.id}"
                                        th:text="${attr.attrName}"></option>
                            </select>
                        </li>
                        <li class="select-time">
                            <p style="width: 110px">加油时间： </p>
                            <input type="text" class="time-input" style="width: 150px;" data-type="datetime" placeholder="开始时间"
                                   name="params[beginTime]"/>
                            <span>-</span>
                            <input type="text" class="time-input" style="width: 150px;" data-type="datetime" placeholder="结束时间"
                                   name="params[endTime]"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="searchPre()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="resetPre()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>
        <div class="btn-group-sm" id="toolbar" role="group">
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: bootstrap-select-js" />
<script th:inline="javascript">
    const prefix = ctx + "back/shopOrder";
    const orderStatusDict = [[${@dict.getType('order_status')}]];
    const discountTypeDict  = [[${@dict.getType('discount_type')}]];
    const payStatusDict = [[${@dict.getType('pay_status')}]];
    const payWayDict = [[${@dict.getType('pay_way')}]];
    const closeFlag = [[${@permission.hasPermi('back:shopOrder:petrolCancel')}]];

    $(function () {
        var options = {
            url: prefix + "/searchPetrolList",
            exportUrl: prefix + "/export",
            height: 600,
            modalName: "会员加油订单",
            sortName: "createTime",
            sortOrder: "desc",
            showColumns: false,
            queryParams: queryParams,
            columns: [
                {
                    field: 'orderSn',
                    title: '订单号',
                    sortable: true
                },
                {
                    field: 'paySn',
                    title: '付款单号',
                    visible: false,
                },
                {
                    field: 'phone',
                    title: '会员信息',
                    formatter: function (value, row, index) {
                        var actions = [];
                        let imgHtml = $.common.sprintf("<img class='img-circle img-xs' data-height='%s' data-width='%s' data-target='%s'  style='border-radius: 0;' src='%s'/>", 500, 'auto', 'self', row.avatar);
                        actions.push(imgHtml);
                        actions.push('<span style="font-weight: bold">' + row.phone + '</span><br/>');
                        return actions.join('');
                    }
                },
                {
                    field: 'productName',
                    title: '油品信息',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<label>油号：</label><span style="font-weight: bold">' + value + '</span>');
                        actions.push('<br/><label>单价：</label><span style="font-weight: bold">' + row.productUnitPrice + ' 元/升</span>');
                        actions.push('<br/><label>数量：</label><span style="font-weight: bold">' + row.productQuantity + '升</span>');
                        return actions.join('');
                    },
                    sortable: true
                },
                {
                    field: 'productAttrName',
                    title: '油枪号',
                    sortable: true
                },
                {
                    field: 'orderCost',
                    title: '金额信息',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<label>加油金额：</label><span style="font-weight: bold">' + value + ' 元</span>');
                        // actions.push('<br/><label>优惠金额：</label><span style="font-weight: bold">-' + row.discountAmount + ' 元</span>');
                        actions.push('<br/><label>实付金额：</label><span style="font-weight: bold">' + row.payCost + ' 元</span>');
                        return actions.join('');
                    }
                },
                {
                    field: 'discountType',
                    title: '优惠信息',
                    visible: false,
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<label>优惠类型：</label><span style="font-weight: bold">' + $.table.selectDictLabel(discountTypeDict, row.discountType)  + '</span>');
                        actions.push('<br/><label>优惠描述：</label><span style="font-weight: bold">' + row.discountTitle + '</span>');
                        return actions.join('');
                    }
                },
                {
                    field: 'orderStatus',
                    title: '订单状态',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(orderStatusDict, value);
                    }
                },
                {
                    field: 'payStatus',
                    title: '支付信息',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<label>支付方式：</label><span style="font-weight: bold">' + $.table.selectDictLabel(payWayDict, row.payWay) + '</span>');
                        actions.push('<br/><label>支付状态：</label><span style="font-weight: bold">' + $.table.selectDictLabel(payStatusDict, value) + '</span>');
                        if (parseInt(value) === 1) {
                            actions.push('<br/><label>支付时间：</label><span style="font-weight: bold">' + row.payTime + '</span>');
                        }
                        return actions.join('');
                    }
                },
                {
                    field: 'createTime',
                    title: '创建时间',
                    sortable: true
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        switch (parseInt(row.orderStatus)) {
                            //未支付
                            case 0:
                                actions.push('<a class="btn btn-success btn-xs ' + closeFlag + '" href="#" onclick="cancel(\'' + row.orderSn + '\',false)"><i class="fa fa-backward">取消订单</i></a> ');
                                break;
                            //已支付
                            case 3:
                                if(row.payWay != 3){
                                    actions.push('<a class="btn btn-danger btn-xs ' + closeFlag + '" href="#" onclick="cancel(\'' + row.orderSn + '\',true)"><i class="fa fa-backward">退款</i></a> ');
                                }
                                break;
                            default:
                                break;
                        }
                        return actions.join('');
                    }
                }
                ]
        };
        $.table.init(options);
    });

    function queryParams(params) {
        var search = $.table.queryParams(params);
        search.productIdStr = $.common.join($('#productIdStr').selectpicker('val'));
        return search;
    }

    function searchPre() {
        $.table.search('petrol-order-form', 'bootstrap-table');
    }

    function resetPre() {
        $("#petrol-order-form")[0].reset();
        $("#productIdStr").selectpicker('refresh');
        $.table.search('petrol-order-form', 'bootstrap-table');
    }

    /* 订单取消 */
    function cancel(orderSn,isPay) {
        let text = isPay?"所选订单【" + orderSn + "】已付款，确认退款吗？":"所选订单【" + orderSn + "】，确认取消吗？";
        $.modal.confirm(text, function () {
            $.post(prefix + "/petrolCancel", {orderSn: orderSn}, function (result) {
                if (result.code == web_status.SUCCESS) {
                    $.modal.alertSuccess(result.msg);
                    $.table.refresh();
                } else if (result.code == web_status.WARNING) {
                    $.modal.alertWarning(result.msg);
                } else {
                    $.modal.alertError(result.msg);
                }
                $.modal.closeLoading();
            });
        });
    }
</script>
</body>
</html>